#============================================================
#	Enemy
#============================================================
# @datetime: 2022-4-21 20:11:44
#============================================================
class_name Enemy
extends RoleBase


var property_wrapper : PropertyWrapper

onready var type_layer : EnemyTypeLayer = $TypeLayer


#============================================================
#   内置
#============================================================
func _enter_tree():
	# 属性包装器
	property_wrapper = PropertyWrapper.new(self) \
		.init_property(property)
	DamageWrapper.new(self)
	# 攻击
	AttackWrapper.new(self) \
		.wrapper_connect("attacked", self, "_attack")


func _ready():
	property_wrapper.connect("property_changed", self, "_property_change")
	property_wrapper.emit_property_changed_signal()
	
	WrapperHelper \
		.get_damage(self) \
		.connect("took_damage", self, "_take_damage")
	
	connect("dead", self, "_dead")


#============================================================
#   自定义
#============================================================
##  属性改变 
## @_property  发生改变的属性值
## @new_value  新的值
func _property_change(property, new_value):
	if property in type_layer.controller:
		type_layer.controller.set(property, new_value)
	if property == "health":
#		Logger.info(self, ['生命值：', new_value])
		if new_value <= 0:
			emit_signal("dead")

##  攻击 
## @data  攻击数据
func _attack(data):
	Logger.info(self, [self, "发出攻击，数据：", data])

##  受到伤害
## @data  
func _take_damage(data: Dictionary):
	Logger.info(self, ['受到伤害。', data])
	property_wrapper.sub_property("health", data['damage'])

## 死亡
func _dead():
	Logger.error(self, [name, " 被击杀"])
	self.queue_free()


